package com.gccloud.design.utils;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.PoolingClientConnectionManager;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

/**
 * HTTP客户端
 * @author QKX
 * @2013-11-19
 */
public class HttpclientUtil {
	/**
	 * 日志.
	 */
	private static Logger logger = LoggerFactory.getLogger(HttpclientUtil.class);
	public static HttpClient getHttpClient() {

		PoolingClientConnectionManager pccm = new PoolingClientConnectionManager();
		pccm.setDefaultMaxPerRoute(20); //每个主机的最大并行链接数
		pccm.setMaxTotal(100);          //客户端总并行链接最大数
		HttpClient httpClient = new DefaultHttpClient(pccm);
		//设置连接超时
		httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 30000);
		//设置返回超时时间
		httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 30000);

		return httpClient;
	}

	/**
	 * 发送POST请求
	 * @param url
	 * @param content
	 * @return
	 */
	public static String sendRequestByPost(String url,String content){

	    HttpClient httpClient = getHttpClient();
		//设置连接超时
//		httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 5000);
		//设置返回超时时间
//		httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 5000);


		String result = null;
		//发送
		HttpPost httpPost = new HttpPost(url);
		httpPost.setHeader("SOAPAction", "");

		try {
			//内容实体
			httpPost.setEntity(new StringEntity(content, "UTF-8"));
			//发送
			HttpResponse httpResponse = httpClient.execute(httpPost);
			HttpEntity entity = httpResponse.getEntity();
			//返回结果转换成字符串
			result = EntityUtils.toString(entity,"UTF-8");
			EntityUtils.consume(entity);

		} catch (UnsupportedEncodingException e) {

			logger.info(e.getMessage());

		} catch (ClientProtocolException e) {

			logger.info(e.getMessage());

		} catch (IOException e) {

			logger.info(e.getMessage());
		}finally{

			httpClient.getConnectionManager().shutdown();

		}


		return result;

	}

	/**
	 * 发送GET请求
	 * @param url
	 * @return
	 */
	public static String sendRequestByGet(String url){

	    HttpClient httpClient = getHttpClient();
		//设置连接超时
//		httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 5000);
		//设置返回超时时间
//		httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 5000);

		String result = null;
		//发送
		HttpGet httpGet = new HttpGet(url);

		try {

			//发送
			HttpResponse httpResponse = httpClient.execute(httpGet);
			HttpEntity entity = httpResponse.getEntity();
			//返回结果转换成字符串
			result = EntityUtils.toString(entity,"UTF-8");
			EntityUtils.consume(entity);
		} catch (UnsupportedEncodingException e) {

			logger.info(e.getMessage());

		} catch (ClientProtocolException e) {

			logger.info(e.getMessage());

		} catch (IOException e) {

			logger.info(e.getMessage());
		}finally{

			httpClient.getConnectionManager().shutdown();

		}


		return result;

	}



}
